<?php

/** Модель для частей приложения, где требуется авторизация 
 * Обрабатывает logout*/
class AuthModel extends Model
{
	private $oPlayer;
	
	public function __construct()
	{
		parent::__construct();
		$this->oPlayer = null;
		
		World::init();
		
		if (Session::has("auth"))
		{
			$auth = Session::get("auth");
			if (array_key_exists("player_id", $auth))
				$this->oPlayer = PlayerRegistry::i()->byId($auth["player_id"]);
		}
		
		if (Input::request("action") == "logout")
		{
			Session::remove("auth");
			header("Location: /");
			die();
		}
	}
	
	public function player() { return $this->oPlayer; }
		
	public function isPlayer() { return !is_null($this->oPlayer); }
	
	public function isAdmin()
	{
		if (!Session::has("auth")) return false;
		$auth = Session::get("auth");
		
		return ($auth["ac_level"] >= Access::ADMIN);
	}
}

?>